﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace UpdateDesktopDocHtmlContent.JSFileMatch
{
    internal class JSFileMatchHelper
    {
        private Random random;
        public string ContentDir { get; internal set; }
        public string m_mdStyleStr { get; internal set; }
        public string NGUrl { get; internal set; }

        private List<string> m_nologinFiles=new List<string>();
        private List<string> m_fileRecords = new List<string>();

        public JSFileMatchHelper()
        {
            m_nologinFiles.Add("素材下载");
            m_nologinFiles.Add("offical");
            m_nologinFiles.Add("安装与部署");
            m_nologinFiles.Add("环境问题跟踪");
        }

        internal void ProcessFile(string LastSelectedDirectory)
        {
            m_fileRecords.Clear();

            random = new Random();
            
            var files = new DirectoryInfo(LastSelectedDirectory).GetFiles("*.html", SearchOption.AllDirectories);
            for (var i = 0; i < files.Length; i++)
            {
                ProcessFile(files[i]);
            }

            SaveRecordFile();
        }
        private void SaveRecordFile()
        {
            var contentDir = Path.Combine(ContentDir, "fileMatch.txt");
            var fs = File.Create(contentDir);
            var sw = new StreamWriter(fs);
            for (var i = 0; i < m_fileRecords.Count; i++)
            {
                sw.WriteLine(m_fileRecords[i]);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        private void ProcessFile(FileInfo file)
        {
            var data = File.ReadAllText(file.FullName);
            int t1 = data.IndexOf("<main");
            var t2 = data.IndexOf("</main");

            bool isNologinFile = IsNologinFile(file);
            if (isNologinFile)
            {
                WriteMdStyleFile(file,data);
                return;
            }

            if (t1 >= 0 && t2 > 0)
            {
                var leftValue = data.Substring(0, t1);
                var rightValue = data.Substring(t2);

                var t3 = data.IndexOf(">", t1);
                var middValue = data.Substring(t3 + 1, t2 - t3 - 1);
                var newFileName = random.Next(1000, 9999) + "-" + middValue.GetHashCode();
                if (isNologinFile)
                {
                    newFileName = random.Next(100, 999) + "-" + middValue.GetHashCode();
                }

                m_fileRecords.Add(file.FullName + ":" + newFileName);

                var leftValue2 = data.Substring(0, t3 + 1);
                var allValue = leftValue2.Replace("<main", "<main code='" + newFileName + "'") + rightValue;
                var t4 = allValue.IndexOf("</html>");

                var mdFile = "<script src='mdStyle.js'></script>";
                mdFile += "<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css\">";
                if (allValue.IndexOf(mdFile) <= 0)
                {
                    allValue = allValue.Substring(0, t4) + mdFile + allValue.Substring(t4);
                }
                allValue = Regex.Replace(allValue, "{NGURL}", NGUrl);
                allValue = Regex.Replace(allValue, "{NGURL}".ToLower(), NGUrl);
                middValue = Regex.Replace(middValue, "{NGURL}", NGUrl);
                middValue = Regex.Replace(middValue, "{NGURL}".ToLower(), NGUrl);

                File.WriteAllText(file.FullName, allValue);
                //中间文件
                File.WriteAllText(Path.Combine(ContentDir, newFileName + ".txt"), middValue);
            }
            var mdStyleJS = Path.Combine(file.Directory.FullName, "mdStyle.js");
            if (File.Exists(mdStyleJS) == false)
            {
                File.WriteAllText(mdStyleJS, m_mdStyleStr);
            }
        }
        private void WriteMdStyleFile(FileInfo file,string allValue)
        {
            var mdFile = "<script src='mdStyle.js'></script>";
            mdFile += "<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css\">";
            var t4 = allValue.IndexOf("</html>");
            if (allValue.IndexOf(mdFile) <= 0)
            {
                allValue = allValue.Substring(0, t4) + mdFile + allValue.Substring(t4);
            }
            File.WriteAllText(file.FullName, allValue);
        }

        private bool IsNologinFile(FileInfo file)
        {
            var fileName = file.Name;
            for(var i=0;i<this.m_nologinFiles.Count;i++)
            {
                if (fileName.StartsWith(m_nologinFiles[i])) return true;
            }
            return false;
        }
    }
}
